package nl.harmidi.swing;

import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.util.prefs.Preferences;

import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import nl.harmidi.main.Application;

public class NoteOffDelayPanel extends JPanel {

	private JSlider noteOffSlider;

	private Application app;

	public NoteOffDelayPanel(Application app) {
		this.app = app;
		initGui();
	}

	private void initGui() {
		setLayout(new BorderLayout());
		createNoteOffDelaySlider();
		add(noteOffSlider, BorderLayout.CENTER);
	}

	private void createNoteOffDelaySlider() {

		int maxDelaySec = Preferences.userRoot().getInt("noteoff.delay.max", 30);

		noteOffSlider = new JSlider(JSlider.HORIZONTAL, 0, maxDelaySec, 0);

		noteOffSlider.setMajorTickSpacing(3);
		noteOffSlider.setMinorTickSpacing(1);
		noteOffSlider.setPaintTicks(true);
		noteOffSlider.setPaintLabels(true);
		noteOffSlider.setSnapToTicks(true);

		noteOffSlider.setToolTipText("Note off delay (seconds)");

		int width = 500;
		int appWidth = Preferences.userRoot().getInt("frame.width", 0);
		width = appWidth - 300;

		noteOffSlider.setBounds(new Rectangle(width, (int) noteOffSlider.getBounds().getHeight()));

		noteOffSlider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				JSlider source = (JSlider) e.getSource();
				if (!source.getValueIsAdjusting()) {
					int newDelay = (int) source.getValue();
					app.setNewDelay(newDelay);
				}
			}
		});

	}

}
